package com.example.spring_java_oj.service;

import com.example.spring_java_oj.Util.FileUtil;
import com.example.spring_java_oj.model.Problem;
import com.example.spring_java_oj.service.problem.ProblemRedisService;
import com.example.spring_java_oj.service.problem.ProblemService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

import static com.example.spring_java_oj.common.Constants.TEMCODE;

@Service
public class ProblemAmountService {
    @Autowired
    private ProblemService problemService;

    @Autowired
    private ProblemRedisService problemRedisService;

    private Problem problem;

    public List<Problem> getList() throws JsonProcessingException {
        List<Problem> list = problemRedisService.getList();
        if (list == null){
            List<Problem> problems = problemService.getList();
            problemRedisService.insertProblems(problems);
        }
        list = problemRedisService.getList();
        return list;
    }

    public Problem getProblem(Integer id, HttpServletRequest request) throws JsonProcessingException {
        Problem problem = problemRedisService.getProblem(id, request);
        if (problem == null){
            problem = problemService.getProblem(id, request);
            if (problem != null){
                problemRedisService.updateAndInsertProblem(problem);
            }else {
                return problem;
            }
        }
        HttpSession session = request.getSession();
        String path ="./tmp/" + session.getAttribute("username") + id + "/";
        File workDir = new File(path);
        if(!workDir.exists()){
            workDir.mkdirs();
        }
        String temPath = path + "finalCodeFile.txt";
        File file = new File(temPath);
        if(!file.exists()){
            return problem;
        }
        String tem = FileUtil.readFile(temPath);
        problem.setTemplateCode(tem);
        return problem;
    }

    public Problem getProblemStart(Integer id) throws JsonProcessingException {
        Problem problem = problemRedisService.getProblemStart(id);
        if (problem == null){
            problem = problemService.getProblemStart(id);
            if(problem != null){
                problemRedisService.updateAndInsertProblem(problem);
            }else {
                problem = new Problem();
            }
        }
        return problem;
    }

    public Integer updateProblem(Problem problem) throws JsonProcessingException {
        if (problemService.getProblem(problem.getId(), null) == null){
            return 2;
        }

        Problem problem1 = problemRedisService.getProblem(problem.getId(), null);
        System.out.println("problem1prev: " + problem1.toString());
        problem1.setTitle(problem.getTitle());
        problem1.setAnswer(problem.getAnswer());
        problem1.setDescription(problem.getDescription());
        problem1.setLevel(problem.getLevel());
        System.out.println("problem1after: " + problem1.toString());
        problemRedisService.updateAndInsertProblem(problem1);
        return problemService.updateProblem(problem);
    }

    public Integer deleteProblem(Integer id){
        problemRedisService.deleteProblem(id);
        return problemService.deleteProblem(id);
    }

    public Integer insert(Problem problem) throws JsonProcessingException {
        int n = problemService.insert(problem);
        problem.setId(problemService.getProblem_Name(problem.getTitle(), problem.getDescription()).getId());
        problemRedisService.updateAndInsertProblem(problem);
        return n;
    }


    public void updateProblemAnswer(Problem problem) throws JsonProcessingException {
        problemService.updateProblemAnswer(problem);
        Problem problem1 = problemService.getProblemStart(problem.getId());
        problemRedisService.updateAndInsertProblem(problem1);
    }

    public Problem getTemCode() throws IOException {
        String temCode = null;
        if (problem == null){
            problem = new Problem();
            String path = "./tmp/temCode.txt";
            File file = new File(path);
            if (!file.exists()){
                file.createNewFile();
                if (!file.exists()){
                    return null;
                }
                FileUtil.writeFile(path, TEMCODE);
            }
            temCode = FileUtil.readFile(path);
        }
        if(temCode != null){
            problem.setTemplateCode(temCode);
        }
        return problem;
    }
}
